package com.xin.springcloudconsumer.controller;

import com.google.gson.Gson;
import com.xin.springcloudconsumer.dto.PageResult;
import com.xin.springcloudconsumer.dto.UserDTO;
import com.xin.springcloudconsumer.enums.ConsumerEnum;
import com.xin.springcloudconsumer.feign.FriendFeign;
import com.xin.springcloudconsumer.feign.UserFeign;
import com.xin.springcloudconsumer.form.MyfriendForm;
import com.xin.springcloudconsumer.utils.JsonUtil;
import com.xin.springcloudconsumer.utils.ResultVOUtils;
import com.xin.springcloudconsumer.utils.SessionUtils;
import com.xin.springcloudconsumer.vo.ResultPage;
import com.xin.springcloudconsumer.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@Controller
@RequestMapping("/friends")
@Slf4j
public class FriendController {
    @Autowired
    private FriendFeign friendFeign;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private Gson gson;
    @Autowired
    private SessionUtils sessionUtils;
    /**
     * 创建好友关系
     * @param myfriendForm
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public ResultVO create(@Valid MyfriendForm myfriendForm,
                           BindingResult bindingResult,
                           Map<String,Object> map,
                           HttpServletRequest request){
        if (bindingResult.hasErrors()) {
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return ResultVOUtils.error(1, bindingResult.getFieldError().getDefaultMessage());
        }
       String uid=sessionUtils.getToken(request);
        myfriendForm.setUid(uid);
        ResultVO resultVO = friendFeign.create(myfriendForm.getUid(),myfriendForm.getFid());
        if (resultVO.getCode() == 0) {
            return ResultVOUtils.success();
        }
        return ResultVOUtils.error(1, ConsumerEnum.CREATE_FRIEND_ERROR.getMessage());
    }

    /**
     * 删除好友
     *
     * @param myfriendForm
     * @return
     */
    @GetMapping("/delete")
    public ResultVO delete(@Valid MyfriendForm myfriendForm,
                           BindingResult bindingResult,
                           Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return ResultVOUtils.error(1,bindingResult.getFieldError().getDefaultMessage());

        }
        ResultVO resultVO = friendFeign.delete(myfriendForm.getUid(),myfriendForm.getFid());
        if (resultVO.getCode() == 0) {
            return ResultVOUtils.success();
        }
        return ResultVOUtils.error(1,ConsumerEnum.DELETE_FRIEND_ERROR.getMessage());
    }

    /**
     * 查找好友列表
     * @param
     * @param pageable
     * @return
     */
    @GetMapping("/findList")
    public ModelAndView findList(@PageableDefault(page = 1,size = 10)Pageable pageable,
                                 HttpServletRequest request,
                                 Map<String,Object> map){
        String uid = sessionUtils.getToken(request);
        ResultPage<UserDTO> resultPage = friendFeign.findMyFriendList(uid, pageable.getPageNumber(), pageable.getPageSize());
        map.put("resultPage",resultPage);
        return new ModelAndView("list/list");
    }
    /**
     * 是否为好友
     * @param uid
     * @param fid
     * @return
     */
    @GetMapping("/findFriendShip")
    public ResultVO findFriendShip(@RequestParam(name = "uid",defaultValue = "")String uid,
                                     @RequestParam(name = "fid",defaultValue = "")String fid){
        ResultVO resultVO = friendFeign.findFriendShip(uid, fid);
        if (resultVO.getCode()==0){
               return ResultVOUtils.success();
        }
        return ResultVOUtils.error(1,ConsumerEnum.NOT_RELATIONSHIP.getMessage());
    }

    /**
     * 模糊查询好友
     * @param nickname
     * @param pageable
     * @param map
     * @return
     */
    @GetMapping("/searchFriend")
    public ModelAndView searchFriend(@RequestParam(name = "nickname",defaultValue = "")String nickname,
                                 @PageableDefault(page = 1,size = 10)Pageable pageable,
                                 Map<String,Object> map){
        ResultPage<UserDTO> resultPage = userFeign.findAllByLikeName(nickname, pageable.getPageNumber(), pageable.getPageSize());
        if (ObjectUtils.isEmpty(resultPage)){
           map.put("message","搜索失败!");
           map.put("returnUrl","/friends/findList");
           return new ModelAndView("common/msg_error",map);
        }
        map.put("resultPage",resultPage);
        map.put("nickname",nickname);
        return new ModelAndView("list/searchlist",map);
    }

    /**
     * 异步模糊查询
     * @param nickname
     * @param pageable
     * @return
     */
    @GetMapping("/searchFriendJson")
    @ResponseBody
    public ResultPage<UserDTO> searchFriend(@RequestParam(name = "nickname",defaultValue = "")String nickname,
                                 @PageableDefault(page = 1,size = 10)Pageable pageable){
        ResultPage<UserDTO> resultPage = userFeign.findAllByLikeName(nickname, pageable.getPageNumber(), pageable.getPageSize());

        return resultPage;
    }

    /**
     * 搜索好友详情
     * @param fid
     * @param request
     * @param map
     * @return
     */
    @GetMapping("/searchFriendDetial")
    public ModelAndView searchFriendDetial(@RequestParam(name = "fid",defaultValue = "")String fid,
                                           HttpServletRequest request,
                                           Map<String,Object> map){
        String token = sessionUtils.getToken(request);
        ResultVO resultVO = userFeign.findOne(fid);
        ResultVO friendShip = friendFeign.findFriendShip(token, fid);
        Integer flag=1;
        if (friendShip.getCode()==0){
            flag=0;
        }
        map.put("flag",flag);
        if (resultVO.getCode() == 0) {
            String json = JsonUtil.toJson(resultVO.getData());
            UserDTO userDTO = gson.fromJson(json, UserDTO.class);
            map.put("userDTO",userDTO);
            return new ModelAndView("detial/userDetial",map);
        }
        map.put("message",ConsumerEnum.USER_NOT_EXIST.getMessage());
        map.put("returnUrl","list/searchlist");
        return new ModelAndView("common/msg_error",map);
    }
}
